<?php

namespace app\common\model;

use think\Model;

class OauthOrderClass extends Model
{
    protected $table = 'oauth_order_class';

    /**
     * 通过订单号ID获取购买的班级
     * @param $orderIds
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getClassByOrder($orderIds)
    {
        $model = static::field('order_id, class_name')
            ->where('order_id', 'in', $orderIds)
            ->select()
            ->toArray();
        $result = [];
        foreach ($model as $key => $value) {
            $result[$value['order_id']][] = $value['class_name'];
        }
        return $result;
    }

    /**
     * 判断多个班级中有没购买过其中一个班级
     * @param $oauthId
     * @param $classIds
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function hasBuy($oauthId, $classIds)
    {
        return static::field('id, class_id')
            ->where(['oauth_id' => $oauthId])
            ->where('class_id', 'in', $classIds)
            ->find();
    }
}